﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;


namespace GlobalGameJam
{
    class SnakeScreen : AGameScreen
    {
        ContentManager content;
        Texture2D _texture;
        SpriteBatch spriteBatch;
        private static SnakeScreen Instance;
        GameplayScreen _game;
        public struct Point
        {
            public int X;
            public int Y;
        }

        public List<Point> Body;
        public int direction;
        public Vector2 max;
        public static bool Move
        {
            get { return move; }
            set { move = value; }
        }
        public static bool move = false;
        public Point newPoint(int x, int y)
        {
            Point p = new Point();
            p.X = x;
            p.Y = y;
            return p;
        }

        public static SnakeScreen getInstance()
        {
            if (Instance == null)
            {
                Instance = new SnakeScreen(600, 400);
            }

            return Instance;
        }

        private SnakeScreen(int x, int y)
        {
            TransitionOnTime = TimeSpan.FromSeconds(0);
            TransitionOffTime = TimeSpan.FromSeconds(0);
            name = "SnakeScreen";
            this.Body = new List<Point>();
            this.Body.Add(this.newPoint(x, y));

            this.Body.Add(this.newPoint(x, y + 10));
            this.Body.Add(this.newPoint(x, y + 20));
            this.direction = 0;
            Initialize(new Vector2(x + 10, y + 10));
        }

        public void Initialize(Vector2 max)
        {
            this.max = max;
        }

        public bool isOnPoint(Point P)
        {
            return this.Body.Contains(P);
        }

        public bool eatMyself()
        {
            for (int i = 1; i < this.Body.Count; i++)
                if (this.Body[0].X == this.Body[i].X && this.Body[0].Y == this.Body[i].Y)
                    return true;
            return false;
        }

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            spriteBatch = ScreenManager.SpriteBatch;
        }

        public override void UnloadContent()
        {
            content.Unload();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            Point p = newPoint(this.Body[0].X, this.Body[0].Y); 
    
            base.Update(gameTime, otherScreenHasFocus, false);

            if (IsActive)
            {
                if (move == true)
                {
                    switch (this.direction)
                    {
                        case 0:
                            if (p.Y + 10 > this.max.Y)
                                this.Body[0] = this.newPoint(p.X, 0);
                            else
                                this.Body[0] = this.newPoint(p.X, p.Y + 10);
                            break;
                        case 1:
                            if (p.Y - 10 < 0)
                                this.Body[0] = this.newPoint(p.X, (int)max.Y);
                            else
                                this.Body[0] = this.newPoint(p.X, p.Y - 10);
                            break;
                        case 2:
                            if (p.X + 10 > this.max.X)
                                this.Body[0] = this.newPoint(0, p.Y);
                            else
                                this.Body[0] = this.newPoint(p.X + 10, p.Y);
                            break;
                        case 3:
                            if (p.X - 10 < 0)
                                this.Body[0] = this.newPoint((int)this.max.X, p.Y);
                            else
                                this.Body[0] = this.newPoint(p.X - 10, p.Y);
                            break;

                    }
                    for (int i = 1; i < this.Body.Count; i++)
                    {
                        Point p1 = this.Body[i];
                        this.Body[i] = p;
                        p = p1;
                    }
                    move = false;
                }
            }
        }

        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            // Look up inputs for the active player profile.
            int playerIndex = (int)ControllingPlayer.Value;

            KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
            GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
            if (input.IsPauseGame(ControllingPlayer))
            {
                ExitScreen();
            }
         }

        public override void Draw(GameTime gameTime)
        {
            _texture = content.Load<Texture2D>("square");
            spriteBatch.Begin();
            for (int i = 0; i < this.Body.Count; i++)
                spriteBatch.Draw(_texture, new Vector2(Body[i].X, Body[i].Y), Color.White);
            spriteBatch.End();
        }
    }
}
